﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DarkCRM.Helpers;
using System.Data;

namespace DarkCRM.Models
{
    public class Custom : Entity
    {
        
        public int CustomID { get; set; }
        public string DisplayName { get; set; }
        public string CustomEmail { get; set; }
        public string MobilePhone { get; set; }
        public string OfficePhone { get; set; }
        public string CompanyName { get; set; }
        public string CompanyAddress { get; set; }
        public string Note { get; set; }
        public int Sex { get; set; } //0 男   1女
        public bool IsDelete { get; set; }
        public int Status { get; set; }
        public string CustomLike { get; set; }
        public string CustomLikeID { get; set; }
        public int MemberID { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime UpdateDate { get; set; }
        public int UpdateBy { get; set; }

        private bool Create()
        {
            IDictionary<string, object> par = new Dictionary<string, object>();
            par.Add("DisplayName", this.DisplayName??"");
            par.Add("CustomMail", this.CustomEmail);
            par.Add("MobilePhone", this.MobilePhone);
            par.Add("OfficePhone", this.OfficePhone??"");
            par.Add("CompanyName", this.CompanyName??"");
            par.Add("CompanyAddress", this.CompanyAddress ?? "");
            par.Add("Note", this.Note ?? "");
            par.Add("Sex", this.Sex);
            par.Add("Status", this.Status);
            par.Add("CustomLike", this.CustomLike ?? "");
            par.Add("CustomLikeID", this.CustomLikeID ?? "");
            par.Add("MemberID", this.MemberID);

            int i = SqlHelper.ExecuteNonQuery("CreateCustom", par);
            return i > 0;
        }

        private static IList<Custom> GetAllCustoms()
        {
            DataTable dt = SqlHelper.ExecuteReader("GetAllCustom").Tables[0];

            EntityList<Custom> customs = new EntityList<Custom>();
            customs.Load(dt);
            return customs;
        }
        private bool CheckExist()
        {
            IDictionary<string, object> par = new Dictionary<string, object>();
            par.Add("CustomEmail", this.CustomEmail);
            par.Add("MobilePhone", this.MobilePhone);
            return SqlHelper.ExecuteScalar("CheckCustomExist", par).ToString() == "1";
        }
        /// <summary>
        /// 检查Name是否存在
        /// </summary>
        /// <returns></returns>
        private static bool CheckcCustomNameExist(string displayName)
        {
            IDictionary<string, object> par = new Dictionary<string, object>();
            par.Add("DisplayName", displayName);
            return SqlHelper.ExecuteScalar("CheckCustomNameExist", par).ToString() == "1";
        }

        private static  bool Delete(int customID)
        {
            IDictionary<string, object> par = new Dictionary<string, object>();
            par.Add("CustomID", customID);
            return SqlHelper.ExecuteNonQuery("DeleteCustom", par) > 0;
        }

        #region for Ajax
        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pagenum"></param>
        /// <returns></returns>
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "get", DataType = "json", Async = false)]
        public static ResultMessage GetPageing(int pagesize, int pagenum)
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request["pagesize"] != null)
                pagesize = int.Parse(request["pagesize"]);
            if (request["pagenum"] != null)
                pagenum = int.Parse(request["pagenum"]);

            var models = GetAllCustoms();
            var ps = models.Select(m => new { ID = m.CustomID, DisplayName = m.DisplayName, CustomEmail = m.CustomEmail, MobilePhone=m.MobilePhone }).Skip((pagenum - 1) * pagesize).Take(pagesize); ;
            var data = new { total = models.Count, rows = ps };
            return new ResultMessage() { ErrorCode = 0, Lable = "success", Value = data };
        }

        /// <summary>
        /// 创建客户
        /// </summary>
        /// <param name="custom"></param>
        /// <returns></returns>
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage Create(Custom custom)
        {
            ResultMessage check = CheckCustomExist(custom);
            if (check.ErrorCode > 0)
                return check;

            return new ResultMessage() { ErrorCode = 0, Lable = "success", Value = custom.Create(),Data=custom };
        }

        /// <summary>
        /// 检查email&mobilephone是否存在
        /// </summary>
        /// <param name="custom"></param>
        /// <returns></returns>
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "get", DataType = "json", Async = false)]
        public static ResultMessage CheckCustomExist(Custom custom)
        {
            if (custom == null)
            {
                return new ResultMessage() { ErrorCode = 1001, Lable = "CUSTOM_NULL" };
            }
            else
            {
                if (custom.CheckExist())
                {
                    return new ResultMessage() { ErrorCode = 1002, Lable = "CUSTOM_EXIST" };
                }
                else
                {
                    return new ResultMessage() { ErrorCode = 0, Lable = "CUSTOM_NOEXIST" };
                }
            }
        }

        /// <summary>
        /// 获取客户名称，用户dropdown 绑定
        /// </summary>
        /// <returns></returns>
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "get", DataType = "json", Async = false)]
        public static ResultMessage GetCustomName()
        {
            var models = GetAllCustoms();
            var ps = models.Select(m => new { value = m.CustomID, text = m.DisplayName}) ;
            return new ResultMessage() { ErrorCode = 0, Lable = "success", Data = ps };
        }
        /// <summary>
        /// 判断客户名称是否存在
        /// </summary>
        /// <returns></returns>
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "get", DataType = "json", Async = false)]
        public static ResultMessage CheckCustomNameExist(string displayName)
        {
            if (string.IsNullOrEmpty(displayName))
            {
                return new ResultMessage() { ErrorCode = 1001, Lable = "DISPLAYNAME_NULL" };
            }
            else
            {
                if (Custom.CheckcCustomNameExist(displayName))
                {
                    return new ResultMessage() { ErrorCode = 1002, Lable = "CUSTOM_NAME_EXIST" };
                }
                else
                {
                    return new ResultMessage() { ErrorCode = 0, Lable = "CUSTOM_NAME_NOEXIST" };
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="custom"></param>
        /// <returns></returns>
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "get", DataType = "json", Async = false)]
        public static ResultMessage DeleteCustom(int customid)
        {
            return new ResultMessage() { ErrorCode = 0, Lable = "SUCCESS", Value = Custom.Delete(customid) };
            
        }
        #endregion
    }

}